﻿--[[
   @brief    lua plugin manager.
   @author   hqzheng
   @version  1.0
   @date     2018-03-26
--]]

lua_plugin_mgr = 
{   
    module_2_plugin_info_list = {};
    event_id_2_proc_func = {};
    lua_plugin_info = {};

    init = function (args)
         -- local test_xml_path = "wegame_mainframe\\navigation_tab\\lua_plugins_info.xml";
         -- local test_list = {};
         -- lua_plugin_mgr.proc_plugin_info(test_xml_path, test_list);
         comm_center.reg_service("i_service_run_lua_plguin", lua_plugin_mgr.on_run_plugin);
         comm_center.reg_service("i_service_stop_lua_plguin", lua_plugin_mgr.on_stop_plugin);
    end;

    -- 注册lua插件
    register_lua_plugin = function (plugin_name, plugin_info)
        base_utility.log(string.format("[register_lua_plugin]plugin_name:%s", plugin_name));
        lua_plugin_mgr.lua_plugin_info[plugin_name] = plugin_info;
    end;

    -- 添加模块的配置信息, 返回插件数
    add_module_plugins = function (module_name, path)
        local plugin_info_list = {};
        if nil ~= module_name and lua_plugin_mgr.proc_plugin_info(path, plugin_info_list) then
            lua_plugin_mgr.module_2_plugin_info_list[module_name] = plugin_info_list;
            lua_plugin_mgr.load_plugin_desc(plugin_info_list);
        end
        return #plugin_info_list;
    end;

    -- 加载模块插件, 模块UI初始化完后调用
    load_plugins = function (module_name)
        local plugins_info = lua_plugin_mgr.module_2_plugin_info_list[module_name];
        if nil == plugins_info then
            return;
        end
        for i = 1, #plugins_info do
            local info = plugins_info[i];
            if nil ~= info then
                local func_script = info.ui_proc_func .. "(\'"  .. tostring(info.ui_info) .. "\')";
                local func = loadstring(func_script);
                -- 执行UI处理函数
                if nil ~= func then
                    func();
                end
                -- 注册驱动事件
                lua_plugin_mgr.register_event(info.action, info.action_proc_func);
            end
        end
    end;

    -- 加载插件事件驱动函数
    register_event = function (event_id, proc_func)
        if nil == event_id or nil == proc_func then
            return;
        end
        lua_plugin_mgr.event_id_2_proc_func[event_id] = proc_func;
        comm_center.listen_broadcast(event_id, "lua_plugin_mgr.handle_event");
    end;

    -- 读取配置文件
    proc_plugin_info = function (xml_path, plugin_info_list)
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(xml_path);
        if nil == xmlparser or nil == xmlparser.plugin_info then
            return false;
        end
        local is_success = true;
        local node_size = #xmlparser.plugin_info.plugin;
        if 0 == node_size then
            local plugin_info = {};
            lua_plugin_mgr.generate_plugin_info(xmlparser.plugin_info.plugin, plugin_info);
            table.insert(plugin_info_list, plugin_info);
        else
            for i = 1, node_size do
                local plugin_info = {};
                local plugin = xmlparser.plugin_info.plugin[i];
                lua_plugin_mgr.generate_plugin_info(plugin, plugin_info);
                table.insert(plugin_info_list, plugin_info);
            end;
        end
        return is_success;
    end;

    generate_plugin_info = function (plugin, plugin_info)
        if plugin ~= nil then
            plugin_info["name"] = plugin["@name"];
            if nil ~= plugin.ui_info then
                plugin_info["ui_info"] = plugin.ui_info["@file"];
            end
            if nil ~= plugin.ui_proc_func then
                plugin_info["ui_proc_func"] = plugin.ui_proc_func["@name"];
            end
            if nil ~= plugin.action then
                plugin_info["action"] = plugin.action["@eventid"];
            end
            if nil ~= plugin.action_proc_func then
                plugin_info["action_proc_func"] = plugin.action_proc_func["@name"];
            end
        end
    end;

    handle_event = function (data)
        if nil == data then
            return;
        end
        local func_script = lua_plugin_mgr.event_id_2_proc_func[data.cmd];
        if nil ~= func_script then
            func_script = func_script .. "()";                
            local func = loadstring(func_script);
            func();
        end
    end;

    get_plugin_run_event = function (module_name, plugin_name)
        local event_id;
        local plugins_info = lua_plugin_mgr.module_2_plugin_info_list[module_name];
        for i = 1, #plugins_info do
            local info = plugins_info[i];
            if nil ~= info and info.name == plugin_name then
                event_id = info.action;
                break;
            end
        end
        return event_id;
    end;

    -- 加载模块的描述文件
    load_plugin_desc = function (plugin_list_tb)
        if nil == plugin_list_tb then
            return;
        end

        local module_list = ierd_tgp.tpf_ui.LuaModuleList:new();
        for i = 1, #plugin_list_tb do
            local module_name = ierd_tgp.tpf_ui.LuaModuleName:new();
            module_name.module_name = plugin_list_tb[i].name;
            module_list.module_list[i-1] = module_name;
            module_name:delete();
        end
        module_list.module_count = #plugin_list_tb;
        ierd_tgp.tpf_ui.LoadDesc(module_list);
        desc_mgr.work();
        module_list:delete();
    end;

    on_run_plugin = function (data)
        base_utility.log("on_run_plugin");
        if nil == data or nil == lua_plugin_mgr.lua_plugin_info then
            return;
        end
        base_utility.log(string.format("[on_run_plugin]plugin_name:%s", data.plugin_name));
        local plugin_info = lua_plugin_mgr.lua_plugin_info[data.plugin_name];
        base_utility.log(string.format("[on_run_plugin]plugin_info:%s", base_utility.table_to_json(plugin_info)));
        if nil ~= plugin_info and nil ~= plugin_info.run_func then
            local func_script = plugin_info.run_func .. "()";                
            local func = loadstring(func_script);
            base_utility.log("on_run_plugin func:" .. func_script);
            func();
        end
    end;

    on_stop_plugin = function (data)
        if nil == data or nil == lua_plugin_mgr.lua_plugin_info then
            return;
        end
        base_utility.log("on_run_plugin");
        local plugin_info = lua_plugin_mgr.lua_plugin_info[data.plugin_name];
        if nil ~= plugin_info and nil ~= plugin_info.stop_func then
            local func_script = plugin_info.stop_func .. "()";                
            local func = loadstring(func_script);
            base_utility.log("on_stop_plugin func:" .. func_script);
            func();
        end
    end;
}

lua_plugin_mgr.init();